package com.question.business.org.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.question.business.org.model.ao.*;
import com.question.business.org.model.vo.OrgAddVO;
import com.question.business.org.model.vo.OrgDetailVO;
import com.question.business.org.model.vo.OrgDropDownVO;
import com.question.business.org.model.vo.OrgVO;
import com.question.business.org.service.IOrgService;
import com.question.common.annotation.DataConvert;
import com.question.common.utils.DateUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


/**
 * 机构接口
 *
 * @author star
 * @since 2023-09-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("/org")
public class OrgController {

    private final IOrgService service;


    /**
     * 机构分页查询
     *
     * @param ao 请求参数
     * @return Page<OrgVO> 响应参数
     */
    @GetMapping("/page")
    public Page<OrgVO> queryOrgPage(@Validated OrgPageAO ao) {
        return service.queryOrgPage(ao);
    }

    /**
     * 机构明细查询
     *
     * @param id 机构ID
     * @return OrgDetailVO 响应参数
     */
    @DataConvert
    @GetMapping("/detail")
    public OrgDetailVO queryOrgDetail(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        return service.queryOrgDetail(id);
    }

    /**
     * 机构新增数据
     *
     * @param ao 请求参数
     * @return OrgAddVO 响应参数
     */
    @PostMapping("/add")
    public OrgAddVO addOrg(@Validated @RequestBody OrgAddAO ao) {
        return service.addOrg(ao);
    }

    /**
     * 机构修改数据
     *
     * @param ao 数据
     */
    @PutMapping("/update")
    public void updateOrg(@Validated @RequestBody OrgUpdateAO ao) {
        service.updateOrg(ao);
    }

    /**
     * 机构删除数据
     *
     * @param id 机构ID
     */
    @DeleteMapping("/delete")
    public void deleteOrg(@Validated @NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String id) {
        service.deleteOrg(id);
    }

    /**
     * 机构批量删除数据
     *
     * @param ids 机构ID
     */
    @DeleteMapping("/batch/delete")
    public void deleteBatchOrg(@RequestParam @Validated @NotNull(message = "删除数据不能为空") @Size(min = 1, message = "删除数据不能为空") List<@NotBlank(message = "删除数据不能为空") @Size(max = 20, message = "ID最大长度不能超过20") String> ids) {
        service.deleteBatchOrg(ids);
    }

    /**
     * 机构导出数据
     *
     * @param ao       请求参数
     * @param response 响应流
     */
    @GetMapping("/export")
    public void exportOrg(OrgPageAO ao, HttpServletResponse response) throws IOException {
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("机构-" + DateUtil.getStringTime() + ".xlsx", "UTF-8"));
        ServletOutputStream outputStream = response.getOutputStream();
        service.exportOrg(ao, outputStream);
        IOUtils.closeQuietly(outputStream);
    }

    /**
     * 机构启用、停用
     *
     * @param ao 请求参数
     */
    @PutMapping("/enable")
    public void enableOrg(@Validated @RequestBody OrgEnabledAO ao) {
        service.enableOrg(ao);
    }

    /**
     * 机构下拉列表查询
     *
     * @param ao 请求参数
     * @return List<OrgDropDownVO> 响应参数
     */
    @GetMapping("/drop/down")
    public List<OrgDropDownVO> queryOrgDropDown(@Validated OrgDropDownAO ao) {
        return service.queryOrgDropDown(ao);
    }

}
